package ee.ut.aa.neuraltic.model;

import java.util.List;
import java.util.Scanner;

import org.apache.log4j.Logger;

public class HumanPlayer implements Player {

	private static Logger log = Logger.getLogger( HumanPlayer.class );

	private int value = -1;

	private Scanner in = new Scanner( System.in );

	@Override
	public Board makeNextMove( Board board ) {

		log.debug( "Requesting next move for player " + value );

		List<Integer> legalMoves = board.getLegalMoves();

		System.out.println( "Board:" );
		System.out.println( board );
		System.out.println( "\nLegal moves:" );
		System.out.println( legalMoves );
		System.out.println( "Enter your move: " );

		int move = in.nextInt();

		System.out.println( "Your move was: " + move );

		log.debug( "Next move is: " + move );

		try {
			board.makeMove( move );
		} catch( Exception e ) {
			System.err.println( "You screwed up this move - Initializing move request." );
			makeNextMove( board );
		}

		if( legalMoves.size() == board.getLegalMoves().size() ) {
			System.err.println( "You screwed up this move - not even trying to fix this one, this game is done." );
			for( int i = 0; i < board.getSize(); i++ ) {
				board.setValue( i, 1 );
			}
		}

		return board;
	}

	@Override
	public int getValue() {

		return value;
	}

	@Override
	public void setValue( int value ) {

		this.value = value;
	}
}
